package io.github.bigbird0101.datatransfer.task.handler;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import integration.data.enums.IntegrationTaskTypeEnum;
import integration.data.model.entity.DataIntegration;
import integration.data.model.entity.TaskDefinition;
import integration.data.utils.CodeGenerateUtils;
import integration.modle.enums.ErrorCodeEnum;
import integration.modle.exception.IntegrationValidException;
import integration.utils.trace.LogUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import static integration.data.constants.IntegrationConstants.*;

/**
 * 集成配置转任务
 */
@Slf4j
public abstract class IntegrationTaskDefHandler {


    public static IntegrationTaskDefHandler getHandler(String integrationType) {
        switch (integrationType) {
            case BATCH:
                return new BatchTaskDefHandler();
            case MIGRATION:
                return new MigrationTaskDefHandler();
            case DATA_MQ:
                return new MQTaskDefHandler();
            case DATA_API:
                return new ApiTaskDefHandler();

//            case UNSTRUCTURED:
//            case UNSTRUCTURED_STREAM:
//                return new UnstructuredTaskDefHandler(processDefinitionService);
//            case STREAM:
//                return new StreamTaskDefHandler(processDefinitionService);
            default:
                LogUtils.warn(log, "不支持此任务类型：{}", integrationType);
                throw new RuntimeException("不支持此任务类型：" + integrationType);
        }

    }

    public static List<TaskDefinition> configToTask(DataIntegration param) {

        String integrationType = param.getIntegrationType();
        List<TaskDefinition> taskDefinitionList = getHandler(integrationType).doTask(param);

        return taskDefinitionList;
    }

    protected String getTaskType(String integrationType) {

        IntegrationTaskTypeEnum taskTypeEnum = IntegrationTaskTypeEnum.of(integrationType);
        if (taskTypeEnum == null) {
            return IntegrationTaskTypeEnum.BATCH.getType();
        }
        return taskTypeEnum.getType();
    }

    public abstract List<TaskDefinition> doTask(DataIntegration param);


    protected TaskDefinition buildTaskDefinitionJson(DataIntegration param) {

        // 集成相关任务类型：STREAM_INTEGRATION, INTERGRATION, STREAM_UNSTRUCTURED, UNSTRUCTURED, MQ_INTERGRATION, API_INTERGRATION
        String taskType = getTaskType(param.getIntegrationType());
//        param.setIntegrationType(taskType);

        String taskParams = buildTaskParam(param);
        long code = 0;
        try {
            code = CodeGenerateUtils.getInstance().genCode();
        } catch (CodeGenerateUtils.CodeGenerateException e) {
            throw new IntegrationValidException(ErrorCodeEnum.DATA_INTEGRATION_TASK_CODE_ERROR);
        }

        TaskDefinition taskDefinition = BeanUtil.copyProperties(param, TaskDefinition.class);
        taskDefinition.setCode(code);
        taskDefinition.setIntegrationId(param.getId());
        taskDefinition.setName(param.getJobName());
        taskDefinition.setDescription(param.getJobDesc());
        taskDefinition.setTaskType(taskType);
        taskDefinition.setTaskParams(taskParams);
        taskDefinition.setScheduleParams(param.getJobCron());

//        TaskDefinition taskDefinition = TaskDefinition.builder()
//                .tenantInstanceId(param.getTenantInstanceId())
//                .userViewType(param.getUserViewType())
//                .applicationId(param.getApplicationId())
//                .timeout(param.getTimeout())
//                .jobId(param.getJobId())
//                .code(code)
//                .integrationId(param.getId())
//                .name(param.getJobName())
//                .description(param.getJobDesc())
//                .taskType(taskType)
//                .taskParams(taskParams)
//                .scheduleParams(param.getJobCron())
//                .build();

        return taskDefinition;
    }


    /**
     * 构建任务参数
     *
     * @param param 任务配置
     * @return
     */
    protected String buildTaskParam(DataIntegration param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("integrationId", param.getId());
        jsonObject.set("applicationId", param.getApplicationId());
        jsonObject.set("taskType", param.getIntegrationType());
        jsonObject.set("jobSrcJson", param.getJobSrcJson());
        jsonObject.set("jobTgtJson", param.getJobTgtJson());
        jsonObject.set("jobMapping", param.getJobMapping());
        jsonObject.set("jobConfig", param.getJobConfig());
        jsonObject.set("transformation", param.getTransformation());
        return jsonObject.toString();
    }


    /**
     * 构建任务参数
     *
     * @param param 任务配置
     * @return
     */
    protected String buildTaskParam(DataIntegration param, String sourceJson, String targetJson, String mapping) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("integrationId", param.getId());
        jsonObject.set("applicationId", param.getApplicationId());
        jsonObject.set("taskType", param.getIntegrationType());
        jsonObject.set("jobSrcJson", sourceJson);
        jsonObject.set("jobTgtJson", targetJson);
        jsonObject.set("jobMapping", mapping);
        jsonObject.set("jobConfig", param.getJobConfig());
        return jsonObject.toString();
    }


}
